#include "bezier.h"

double berstein (int i,int m,double t)
{
  int binom=binomial(m,i);
  double ret=binom*pow(t,(double)i)*pow(1.-t,(double)(m-i));
  return ret;
}

int binomial (int m,int i)
{
/* pas optimise ici */
  return fact(m)/(fact(m-i)*fact(i));
}

double fact (int n)
{
  if (n==0)
    return 1;
  else 
    return fact(n-1)*n;
}

void drawBezier (Point tab [4],int slices)
{
  int count;
  double c0,c1,c2,c3;
  double t=0.;
  double pasT=1./(double)slices;
  Point final;
  Point last;
  glBegin(GL_LINES);
  for (count=0;count<slices;count++)
    {
      last=final;
      c0=berstein(0,3,t);
      c1=berstein(1,3,t);
      c2=berstein(2,3,t);
      c3=berstein(3,3,t);
      final=multVector(tab[0],c0);
      final=addVector(final,multVector(tab[1],c1));
      final=addVector(final,multVector(tab[2],c2));
      final=addVector(final,multVector(tab[3],c3));

      if (count!=0) {
	glVertex3f(last.x,last.y,last.z);
	glVertex3f(final.x,final.y,final.z);
      }
      t+=pasT;
    }
  glEnd();
}

Point addVector(Point A,Point B)
{
  return pt(A.x+B.x,A.y+B.y,A.z+B.z);
}
Point multVector(Point A,double c)
{
  return pt(A.x*c,A.y*c,A.z*c);
}

